package com.hexiang;

import com.hexiang.domain.OmsOrder;
import com.hexiang.spring.dao.JdbcTemplateOmsOrderDaoImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Order(2)
public class BenchmarkDataInitRunner implements ApplicationRunner {

	@Autowired
	private JdbcTemplateOmsOrderDaoImpl jdbcTemplateOmsOrderDaoImpl;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		List<OmsOrder> omsOrderList = new ArrayList<>();
		for (int i = 0; i < SpringBootDaoBenchmarkApplication.DATA_SIZE; i++) {
			OmsOrder omsOrder = new OmsOrder();
			omsOrder.setMemberId(1L);
			String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
			omsOrder.setOrderSn(time + RandomStringUtils.randomNumeric(5));
			omsOrder.setCreateTime(Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()));
			omsOrder.setMemberUsername("李鹏");
			omsOrder.setTotalAmount(new BigDecimal("52.00"));
			omsOrder.setPayAmount(new BigDecimal("57.00"));
			omsOrder.setFreightAmount(new BigDecimal("5.00"));
			omsOrder.setPayType(0);
			omsOrder.setSourceType(1);
			omsOrder.setStatus(0);
			omsOrder.setOrderType(0);
			omsOrderList.add(omsOrder);
		}
		jdbcTemplateOmsOrderDaoImpl.batchSave(omsOrderList);
		System.out.println("init================");
	}
}
